<?php
namespace common\models;


class TradeCommodity extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'trade_commodity';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type', 'money_up', 'money_down',  'month_income', 'sell_start_time', 'sell_end_time'], 'required'],
            [['type'], 'integer'],
            [['money_up', 'money_down', 'day_income', 'month_income','finalist_income','zhitui_income'], 'number'],
            [['sell_start_time', 'sell_end_time', 'create_time', 'update_time'], 'safe'],
            [['img', 'period'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'commodity_id'  => '产品id',
            'type'          => '产品类型 1=>自由理财  2=》合约理财 3=》拼团合约理财',
            'money_up'      => '金额上限',
            'money_down'    => '金额下限',
            'img'           => '图片',
            'day_income'    => '日利率',
            'month_income'  => '月利率',
            'sell_start_time'   => '销售开始时间',
            'sell_end_time'     => '销售结束时间',
            'period'            => '签约周期（自由理财无）',
            'group_id'          => '拼团配置表id',
            'create_time'       => '创建时间',
            'update_time'       => '修改时间',
            'finalist_income'   =>'拼团入围收益',
            'zhitui_income'     =>'直推奖比例'
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
    {
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];

		$data  = $model->asarray()->all();

		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;

		/*拼团处理*/

        $i = 1;
        $j = 1;
        $h = 1;
        foreach ($data as $k=>&$v) {
            foreach ($extends as $extend) {
                if ($extend == 'group') {

                    if ($v['type']==1)
                    {

                        $v['name'] = '自由理财NO.'.$i;
                        $i++;
                    } elseif ($v['type']==2) {

                        $v['name'] = '合约理财NO.' . $j;
                        $j++;
                    }else{

                        $v['name'] = '拼团理财NO.' . $h;

                        $h++;

                       $where = 'commodity_id ='.$v['commodity_id']." 
                and group_time ='".date('Y-m-d')."'
                and member_id REGEXP '^".$params['memberId'].",|,".$params['memberId'].",|,".$params['memberId']."$|^".$params['memberId']."'";

                        $groups = GroupDetail::getOne($where);
                        if ($groups){
                            $v['is_join'] =1;
                        }
                    }
                        $v['stock']='>9999.00';
                        $v['money'] = $v['money_up']!='0.00'?$v['money_up'].'-'.$v['money_down']:$v['money_up'];

                    }
                }
            }

		return $data;
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

}